package com.dfdchem.imd.kaiwu.manager;

import com.dfdchem.imd.kaiwu.util.Log;
import com.dfdchem.imd.kaiwu.util.StringUtil;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 登录授权拦截器
 * @author yqq
 */
public class AuthInterceptor implements Interceptor {
    private static final String TAG = "AuthInterceptor";

    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request request = chain.request();
        String header = request.header("X-Token");
        String xToken = DataManager.getInstance().getXToken();

        if (StringUtil.isEmpty(header) && !StringUtil.isEmpty(xToken)) {
            Request newRequest = chain.request().newBuilder()
                    .addHeader("X-Token", xToken)
                    .build();
            return chain.proceed(newRequest);
        } else {
            return chain.proceed(request);
        }
    }
}
